﻿@page "/board/{BoardId}"
@page "/board/{BoardId}/task/{TaskId}"
@using TaskoMask.Clients.UserPanel.Pages.Boards.Components
@inject BoardApiService BoardApiService
@implements IDisposable

<style>
    body {
        background-color: #007bff !important;
    }
</style>

@if (DataIsloading)
{
    <div class="spinner-loading"></div>
}
else if (Model!=null)
{
    <div id="board_page">
        <div class="col-12 mx-auto">
            <Breadcrumb Model=@Model.Board></Breadcrumb>
            <Cards Model=@Model.Cards BoardId=@Model.Board.Id></Cards>
        </div>
    </div>

}


@code {

    [Parameter]
    public string BoardId { get; set; }

    [Parameter]
    public string TaskId { get; set; }

    public BoardDetailsViewModel Model { get; set; }

    bool DataIsloading = true;



    /// <summary>
    /// /
    /// </summary>
    protected override async Task OnInitializedAsync()
    {
        ComponentMessageService.OnMessage += ReloadDataHandler;

        await LoadDataAsync();

        if (!string.IsNullOrEmpty(TaskId))
             ShowTaskDetailsModal(TaskId);
    }



    /// <summary>
    /// /
    /// </summary>
    private async Task LoadDataAsync()
    {
        DataIsloading = true;

        var boardDetailsListResult = await BoardApiService.GetDetailsAsync(BoardId);
        if (!boardDetailsListResult.IsSuccess)
            ToastService.ShowError(boardDetailsListResult.Errors.ParseToFragment(), boardDetailsListResult.Message);
        else
            Model = boardDetailsListResult.Value;

        DataIsloading = false;

    }



    /// <summary>
    ///This is a handler to be notified when some interesting events happened
    /// </summary>
    private async void ReloadDataHandler(MessageType messageType)
    {
        try
        {
            if (ReloadDataHelper.Board_Index_Need_Reload(messageType))
            {
                await LoadDataAsync();
                StateHasChanged();
            }

        }
        catch
        {
            //Async task failure because of async void challenges
        }
    }



    /// <summary>
    ///
    /// </summary>
    private void ShowTaskDetailsModal(string taskId)
    {
        var parameters = new ModalParameters();
        parameters.Add(nameof(Pages.Tasks.Components.Details.TaskId), taskId);
        ModalService.Show<Pages.Tasks.Components.Details>("", parameters, new ModalOptions { Class = "blazored-modal w-100 h-100 bg-light-gray" });
    }



    /// <summary>
    ///
    /// </summary>
    public void Dispose()
    {
        // unsubscribe from OnMessage event
        ComponentMessageService.OnMessage -= ReloadDataHandler;
    }



}
